var request = require('superagent-charset');
var superagent = require('superagent');
var cheerio = require('cheerio');
var async = require("async");
var fs = require("fs");
var mysql = require("mysql");
request(superagent);

//连接数据库
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'node_api',
});
connection.connect();


var arr = []; //电影详细页链接
//var url = "http://www.mp4pa.com/top/rank.html";
var url = "http://www.mp4pa.com/";
superagent.get(url).end(function(error, mes) {
    if (error) {
        console.log("出问题了1:" + error);
    } else {
        var getDate = new Date();
        var day = getDate.toLocaleDateString().substring(5);
        var $ = cheerio.load(mes.text);

        var list = $('.layout_newlist5 .layout_newlist4').eq(1).find('table');
        $(list).each(function() {
            var _url = $(this).find('td').eq(2).find('a').attr('href')
            var _day = $(this).find('td').eq(0).text().substring(1)
            if (_url != undefined&&_day==day) {
                superagent.get(_url).charset('gbk').end(function(err, res) {
                    if (err) {
                        console.log(_url + "出问题了1:");
                    } else {
                        var movie_name, //名称
                            movie_logo, //图片
                            movie_age = 2017, //年代
                            movie_public_time = '2017-12-12', //上线时间
                            movie_country = '无', //国家
                            movie_language = '无', //国家
                            movie_type = 7, //类型
                            movie_length = 45, //时长
                            movie_actors = '无', //主演
                            skim_number = '无', //集数
                            skim_time = '无', //首播时间
                            movie_desc, //简介
                            movie_baiduyun = '无', //百度
                            movie_bt = '无', //bg
                            movie_thunder = '无' //迅雷
                        movie_spare = '无' //备用
                        var $$ = cheerio.load(res.text);
                        movie_name = $$('h1').text()

                        movie_logo = $$("#text img").attr('src')

                        var movie_desc = $$("#text div").eq(0).text()

                        $$("#text a").each(function() {
                            if ($(this).attr('href').indexOf('baidu') >= 0) {
                                movie_baiduyun = $(this).attr('href')
                            } else if ($(this).attr('href').indexOf('wanpan') >= 0) {
                                movie_thunder = $(this).attr('href')
                            } else if ($(this).attr('href').indexOf('bt4') >= 0) {
                                movie_bt = $(this).attr('href')
                            } else if ($(this).attr('href').indexOf('api') >= 0) {
                                movie_spare = $(this).attr('href')
                            }
                        })

                        $$("#text div").each(function() {
                            if ($(this).text().indexOf('类型') == 0) {
                                if ($(this).text().indexOf('喜剧') == 4) {
                                    movie_type = 1
                                } else if ($(this).text().indexOf('惊悚') == 4) {
                                    movie_type = 2
                                } else if ($(this).text().indexOf('战争') == 4) {
                                    movie_type = 3
                                } else if ($(this).text().indexOf('爱情') == 4) {
                                    movie_type = 4
                                } else if ($(this).text().indexOf('科幻') == 4) {
                                    movie_type = 5
                                } else if ($(this).text().indexOf('动作') == 4) {
                                    movie_type = 6
                                } else if ($(this).text().indexOf('犯罪') == 4) {
                                    movie_type = 7
                                } else if ($(this).text().indexOf('剧情') == 4) {
                                    movie_type = 8
                                }
                            } else if ($(this).text().indexOf('集数') == 0) {
                                var number = $(this).text().match(/\d{2}/g);
                                if (number != null) {
                                    skim_number = number[0]
                                }
                            } else if ($(this).text().indexOf('首播') == 0) {
                                var time = $(this).text().match(/\d{4}-\d{2}-\d{2}/g);

                                if (time != null) {
                                    movie_public_time = time[0]
                                }
                                var day = $(this).text().match(/\d{4}/g);
                                if (day != null) {
                                    movie_age = day[0]
                                }
                            } else if ($(this).text().indexOf('单集片长') == 0) {
                                var long = $(this).text().match(/\d{2}/g);
                                if (long != null) {
                                    skim_time = long[0]
                                }
                            } else if ($(this).text().indexOf('主演') == 0) {
                                movie_actors = $(this).text().substring(3)
                            } else if ($(this).text().indexOf('制片国家') == 0) {
                                movie_country = $(this).text().substring(8)
                            } else if ($(this).text().indexOf('语言') == 0) {
                                movie_language = $(this).text().substring(3)
                            }

                        })

                        var userinto = 'INSERT INTO china_tvs(movie_name,movie_logo,movie_age,movie_public_time,movie_country,movie_language,movie_type,movie_length,movie_actors,movie_desc,movie_baiduyun,movie_bt,movie_thunder,movie_spare,skim_number,skim_time) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
                        var userinto_Params = [movie_name, movie_logo, movie_age, movie_public_time, movie_country, movie_language, movie_type, movie_length, movie_actors, movie_desc, movie_baiduyun, movie_bt, movie_thunder, movie_spare, skim_number, skim_time];
                        connection.query(userinto, userinto_Params, function(err, result) {
                            if (err) {
                                console.log('[ERROR] - ', err.message);
                                return;
                            } else {
                                console.log("成功加入movies");
                            }
                        });
                    }
                })
            }
        })
        /*        $(".titl").each(function(){

                    var _url="http://www.mp4pa.com"+$(this).attr('href')
                    superagent.get(_url).charset('gbk').end(function(err,res){
                        if(err){
                            console.log(_url+"出问题了1:");
                        }else{
                            var movie_name,//名称
                            movie_logo,//图片
                            movie_age=2017,//年代
                            movie_public_time='2017-12-12',//上线时间
                            movie_country='无',//国家
                            movie_language='无',//国家
                            movie_type=7,//类型
                            movie_length=45,//时长
                            movie_actors='无',//主演
                            skim_number='无',//集数
                            skim_time='无',//首播时间
                            movie_desc,//简介
                            movie_baiduyun='无',//百度
                            movie_bt='无',//bg
                            movie_thunder='无'//迅雷
                            movie_spare='无'//备用
                            var $$ = cheerio.load(res.text);
                            movie_name=$$('h1').text()

                            movie_logo=$$("#text img").attr('src')

                            var movie_desc=$$("#text div").eq(0).text()

                            $$("#text a").each(function(){
                                if($(this).attr('href').indexOf('baidu')>=0){
                                    movie_baiduyun=$(this).attr('href')
                                }else if($(this).attr('href').indexOf('wanpan')>=0){
                                    movie_thunder=$(this).attr('href')
                                }else if($(this).attr('href').indexOf('bt4')>=0){
                                    movie_bt=$(this).attr('href')
                                }else if($(this).attr('href').indexOf('api')>=0){
                                    movie_spare=$(this).attr('href')
                                }
                            })

                            $$("#text div").each(function(){
                                if($(this).text().indexOf('类型')==0){
                                    if($(this).text().indexOf('喜剧')==4){
                                        movie_type=1
                                    }else if($(this).text().indexOf('惊悚')==4){
                                        movie_type=2
                                    }else if($(this).text().indexOf('战争')==4){
                                        movie_type=3
                                    }else if($(this).text().indexOf('爱情')==4){
                                        movie_type=4
                                    }else if($(this).text().indexOf('科幻')==4){
                                        movie_type=5
                                    }else if($(this).text().indexOf('动作')==4){
                                        movie_type=6
                                    }else if($(this).text().indexOf('犯罪')==4){
                                        movie_type=7
                                    }else if($(this).text().indexOf('剧情')==4){
                                        movie_type=8
                                    }
                                }else if($(this).text().indexOf('集数')==0){
                                    var number=$(this).text().match(/\d{2}/g);
                                    if(number!=null){
                                        skim_number=number[0]
                                    }
                                }else if($(this).text().indexOf('首播')==0){
                                    var time=$(this).text().match(/\d{4}-\d{2}-\d{2}/g);

                                    if(time!=null){
                                        movie_public_time=time[0]
                                    }
                                    var day=$(this).text().match(/\d{4}/g);
                                    if(day!=null){
                                        movie_age=day[0]
                                    }
                                }else if($(this).text().indexOf('单集片长')==0){
                                    var long=$(this).text().match(/\d{2}/g);
                                    if(long!=null){
                                        skim_time=long[0]
                                    }
                                }else if($(this).text().indexOf('主演')==0){
                                    movie_actors=$(this).text().substring(3)
                                }else if($(this).text().indexOf('制片国家')==0){
                                    movie_country=$(this).text().substring(8)  
                                }else if($(this).text().indexOf('语言')==0){
                                    movie_language=$(this).text().substring(3)  
                                }
                                
                            })

                            var userinto = 'INSERT INTO china_tvs(movie_name,movie_logo,movie_age,movie_public_time,movie_country,movie_language,movie_type,movie_length,movie_actors,movie_desc,movie_baiduyun,movie_bt,movie_thunder,movie_spare,skim_number,skim_time) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
                            var userinto_Params = [movie_name,movie_logo,movie_age,movie_public_time,movie_country,movie_language,movie_type,movie_length,movie_actors,movie_desc,movie_baiduyun,movie_bt,movie_thunder,movie_spare,skim_number,skim_time];
                            connection.query(userinto, userinto_Params, function(err, result) {
                                if (err) {
                                    console.log('[ERROR] - ', err.message);
                                    return;
                                } else {
                                    console.log("成功加入movies");
                                }
                            });
                        }
                    })

                });*/

    };
});